import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  void _incrementCounter() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        color: Colors.orange[100],
        child: MyRenderBox(
          distance: 4.0,
          child: FlutterLogo(size: 200),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {}
}

class MyRenderBox extends SingleChildRenderObjectWidget {
  final double distance;

  MyRenderBox({
    required Widget child,
    required this.distance,
  }) : super(child: child);

  @override
  RenderObject createRenderObject(BuildContext context) {
    // TODO: implement createRenderObject
    return RenderMyRenderBox(distance);
  }

  // @override
  // void updateRenderObject(
  //     BuildContext context, covariant RenderObject renderObject) {
  //   // TODO: implement updateRenderObject
  //   renderObject.distance = distance;
  // }
}

class RenderMyRenderBox extends RenderBox with RenderObjectWithChildMixin {
  double distance;

  RenderMyRenderBox(this.distance);
  @override
  void performLayout() {
    // TODO: implement performLayout
    child?.layout(constraints, parentUsesSize: true);
    size = (child as RenderBox).size;
  }

  //layout 和 paint 是不同的遍历组件树
  @override
  void paint(PaintingContext context, Offset offset) {
    // TODO: implement paint
    context.paintChild(child!, offset);
    context.pushOpacity(offset, 129, (context, offset) {
      context.paintChild(child!, offset + Offset(distance, distance));
    });
  }
}
